home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / u_man / cat1 / perlxs.Z / perlxs
Encoding:
Text File  |  1998-10-28  |  64.2 KB  |  1,981 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       perlxs - XS language reference manual
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.       IIIInnnnttttrrrroooodddduuuuccccttttiiiioooonnnn
  13.  
  14.       XS is    a language used    to create an extension interface
  15.       between Perl and some    C library which    one wishes to use with
  16.       Perl.     The XS    interface is combined with the library to
  17.       create a new library which can be linked to Perl.  An    XXXXSSSSUUUUBBBB
  18.       is a function    in the XS language and is the core component
  19.       of the Perl application interface.
  20.  
  21.       The XS compiler is called xxxxssssuuuubbbbpppppppp.  This compiler will    embed
  22.       the constructs necessary to let an XSUB, which is really a C
  23.       function in disguise,    manipulate Perl    values and creates the
  24.       glue necessary to let    Perl access the    XSUB.  The compiler
  25.       uses ttttyyyyppppeeeemmmmaaaappppssss    to determine how to map    C function parameters
  26.       and variables    to Perl    values.     The default typemap handles
  27.       many common C    types.    A supplement typemap must be created
  28.       to handle special structures and types for the library being
  29.       linked.
  30.  
  31.       See the _p_e_r_l_x_s_t_u_t manpage for    a tutorial on the whole
  32.       extension creation process.
  33.  
  34.       Note:    For many extensions, Dave Beazley's SWIG system
  35.       provides a significantly more    convenient mechanism for
  36.       creating the XS glue code. See the section on
  37.       /_w_w_w._c_s._u_t_a_h._e_d_u/~_b_e_a_z_l_e_y/_S_W_I_G in the    _h_t_t_p: manpage for more
  38.       information.
  39.  
  40.       OOOOnnnn TTTThhhheeee RRRRooooaaaadddd
  41.  
  42.       Many of the examples which follow will concentrate on
  43.       creating an interface    between    Perl and the ONC+ RPC bind
  44.       library functions.  The _r_p_c_b__g_e_t_t_i_m_e() function is used to
  45.       demonstrate many features of the XS language.     This function
  46.       has two parameters; the first    is an input parameter and the
  47.       second is an output parameter.  The function also returns a
  48.       status value.
  49.  
  50.           bool_t rpcb_gettime(const char *host,    time_t *timep);
  51.  
  52.       From C this function will be called with the following
  53.       statements.
  54.  
  55.            #include    <rpc/rpc.h>
  56.            bool_t status;
  57.            time_t timep;
  58.            status =    rpcb_gettime( "localhost", &timep );
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  71.  
  72.  
  73.  
  74.       If an    XSUB is    created    to offer a direct translation between
  75.       this function    and Perl, then this XSUB will be used from
  76.       Perl with the    following code.     The $status and $timep
  77.       variables will contain the output of the function.
  78.  
  79.            use RPC;
  80.            $status = rpcb_gettime( "localhost", $timep );
  81.  
  82.       The following    XS file    shows an XS subroutine,    or XSUB, which
  83.       demonstrates one possible interface to the _r_p_c_b__g_e_t_t_i_m_e()
  84.       function.  This XSUB represents a direct translation between
  85.       C and    Perl and so preserves the interface even from Perl.
  86.       This XSUB will be invoked from Perl with the usage shown
  87.       above.  Note that the    first three #include statements, for
  88.       EXTERN.h, perl.h, and    XSUB.h,    will always be present at the
  89.       beginning of an XS file.  This approach and others will be
  90.       expanded later in this document.
  91.  
  92.            #include    "EXTERN.h"
  93.            #include    "perl.h"
  94.            #include    "XSUB.h"
  95.            #include    <rpc/rpc.h>
  96.  
  97.            MODULE =    RPC  PACKAGE = RPC
  98.  
  99.            bool_t
  100.            rpcb_gettime(host,timep)
  101.             char *host
  102.             time_t &timep
  103.             OUTPUT:
  104.             timep
  105.  
  106.       Any extension    to Perl, including those containing XSUBs,
  107.       should have a    Perl module to serve as    the bootstrap which
  108.       pulls    the extension into Perl.  This module will export the
  109.       extension's functions    and variables to the Perl program and
  110.       will cause the extension's XSUBs to be linked    into Perl.
  111.       The following    module will be used for    most of    the examples
  112.       in this document and should be used from Perl    with the use
  113.       command as shown earlier.  Perl modules are explained    in
  114.       more detail later in this document.
  115.  
  116.            package RPC;
  117.  
  118.            require Exporter;
  119.            require DynaLoader;
  120.            @ISA = qw(Exporter DynaLoader);
  121.            @EXPORT = qw( rpcb_gettime );
  122.  
  123.            bootstrap RPC;
  124.            1;
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  137.  
  138.  
  139.  
  140.       Throughout this document a variety of    interfaces to the
  141.       _r_p_c_b__g_e_t_t_i_m_e() XSUB will be explored.     The XSUBs will    take
  142.       their    parameters in different    orders or will take different
  143.       numbers of parameters.  In each case the XSUB    is an
  144.       abstraction between Perl and the real    C _r_p_c_b__g_e_t_t_i_m_e()
  145.       function, and    the XSUB must always ensure that the real
  146.       _r_p_c_b__g_e_t_t_i_m_e() function is called with the correct
  147.       parameters.  This abstraction    will allow the programmer to
  148.       create a more    Perl-like interface to the C function.
  149.  
  150.       TTTThhhheeee AAAAnnnnaaaattttoooommmmyyyy ooooffff aaaannnn XXXXSSSSUUUUBBBB
  151.  
  152.       The following    XSUB allows a Perl program to access a C
  153.       library function called _s_i_n().  The XSUB will    imitate    the C
  154.       function which takes a single    argument and returns a single
  155.       value.
  156.  
  157.            double
  158.            sin(x)
  159.          double    x
  160.  
  161.       When using C pointers    the indirection    operator * should be
  162.       considered part of the type and the address operator &
  163.       should be considered part of the variable, as    is
  164.       demonstrated in the _r_p_c_b__g_e_t_t_i_m_e() function above.  See the
  165.       section on typemaps for more about handling qualifiers and
  166.       unary    operators in C types.
  167.  
  168.       The function name and    the return type    must be    placed on
  169.       separate lines.
  170.  
  171.         INCORRECT                 CORRECT
  172.  
  173.         double sin(x)             double
  174.           double x                 sin(x)
  175.                            double x
  176.  
  177.       The function body may    be indented or left-adjusted.  The
  178.       following example shows a function with its body left-
  179.       adjusted.  Most examples in this document will indent    the
  180.       body.
  181.  
  182.         CORRECT
  183.  
  184.         double
  185.         sin(x)
  186.         double x
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  203.  
  204.  
  205.  
  206.       TTTThhhheeee AAAArrrrgggguuuummmmeeeennnntttt SSSSttttaaaacccckkkk
  207.  
  208.       The argument stack is    used to    store the values which are
  209.       sent as parameters to    the XSUB and to    store the XSUB's
  210.       return value.     In reality all    Perl functions keep their
  211.       values on this stack at the same time, each limited to its
  212.       own range of positions on the    stack.    In this    document the
  213.       first    position on that stack which belongs to    the active
  214.       function will    be referred to as position 0 for that
  215.       function.
  216.  
  217.       XSUBs    refer to their stack arguments with the    macro SSSSTTTT((((xxxx)))),
  218.       where    _x refers to a position in this XSUB's part of the
  219.       stack.  Position 0 for that function would be    known to the
  220.       XSUB as _S_T(0).  The XSUB's incoming parameters and outgoing
  221.       return values    always begin at    _S_T(0).    For many simple    cases
  222.       the xxxxssssuuuubbbbpppppppp compiler will generate the    code necessary to
  223.       handle the argument stack by embedding code fragments    found
  224.       in the typemaps.  In more complex cases the programmer must
  225.       supply the code.
  226.  
  227.       TTTThhhheeee RRRREEEETTTTVVVVAAAALLLL VVVVaaaarrrriiiiaaaabbbblllleeee
  228.  
  229.       The RETVAL variable is a magic variable which    always matches
  230.       the return type of the C library function.  The xxxxssssuuuubbbbpppppppp
  231.       compiler will    supply this variable in    each XSUB and by
  232.       default will use it to hold the return value of the C
  233.       library function being called.  In simple cases the value of
  234.       RETVAL will be placed    in _S_T(0) of the    argument stack where
  235.       it can be received by    Perl as    the return value of the    XSUB.
  236.  
  237.       If the XSUB has a return type    of void    then the compiler will
  238.       not supply a RETVAL variable for that    function.  When    using
  239.       the PPCODE: directive    the RETVAL variable is not needed,
  240.       unless used explicitly.
  241.  
  242.       If PPCODE: directive is not used, void return    value should
  243.       be used only for subroutines which do    not return a value,
  244.       _e_v_e_n _i_f CODE:     directive is used which sets _S_T(0)
  245.       explicitly.
  246.  
  247.       Older    versions of this document recommended to use void
  248.       return value in such cases. It was discovered    that this
  249.       could    lead to    segfaults in cases when    XSUB was _t_r_u_e_l_y    void.
  250.       This practice    is now deprecated, and may be not supported at
  251.       some future version. Use the return value SV * in such
  252.       cases. (Currently xsubpp contains some heuristic code    which
  253.       tries    to disambiguate    between    "truely-void" and "old-
  254.       practice-declared-as-void" functions.    Hence your code    is at
  255.       mercy    of this    heuristics unless you use SV * as return
  256.       value.)
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  269.  
  270.  
  271.  
  272.       TTTThhhheeee MMMMOOOODDDDUUUULLLLEEEE KKKKeeeeyyyywwwwoooorrrrdddd
  273.  
  274.       The MODULE keyword is    used to    start the XS code and to
  275.       specify the package of the functions which are being
  276.       defined.  All    text preceding the first MODULE    keyword    is
  277.       considered C code and    is passed through to the output
  278.       untouched.  Every XS module will have    a bootstrap function
  279.       which    is used    to hook    the XSUBs into Perl.  The package name
  280.       of this bootstrap function will match    the value of the last
  281.       MODULE statement in the XS source files.  The    value of
  282.       MODULE should    always remain constant within the same XS
  283.       file,    though this is not required.
  284.  
  285.       The following    example    will start the XS code and will    place
  286.       all functions    in a package named RPC.
  287.  
  288.            MODULE =    RPC
  289.  
  290.  
  291.       TTTThhhheeee PPPPAAAACCCCKKKKAAAAGGGGEEEE KKKKeeeeyyyywwwwoooorrrrdddd
  292.  
  293.       When functions within    an XS source file must be separated
  294.       into packages    the PACKAGE keyword should be used.  This
  295.       keyword is used with the MODULE keyword and must follow
  296.       immediately after it when used.
  297.  
  298.            MODULE =    RPC  PACKAGE = RPC
  299.  
  300.            [ XS code in package RPC    ]
  301.  
  302.            MODULE =    RPC  PACKAGE = RPCB
  303.  
  304.            [ XS code in package RPCB ]
  305.  
  306.            MODULE =    RPC  PACKAGE = RPC
  307.  
  308.            [ XS code in package RPC    ]
  309.  
  310.       Although this    keyword    is optional and    in some    cases provides
  311.       redundant information    it should always be used.  This
  312.       keyword will ensure that the XSUBs appear in the desired
  313.       package.
  314.  
  315.       TTTThhhheeee PPPPRRRREEEEFFFFIIIIXXXX KKKKeeeeyyyywwwwoooorrrrdddd
  316.  
  317.       The PREFIX keyword designates    prefixes which should be
  318.       removed from the Perl    function names.     If the    C function is
  319.       rpcb_gettime() and the PREFIX    value is rpcb_ then Perl will
  320.       see this function as gettime().
  321.  
  322.       This keyword should follow the PACKAGE keyword when used.
  323.       If PACKAGE is    not used then PREFIX should follow the MODULE
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  335.  
  336.  
  337.  
  338.       keyword.
  339.  
  340.            MODULE =    RPC  PREFIX = rpc_
  341.  
  342.            MODULE =    RPC  PACKAGE = RPCB  PREFIX = rpcb_
  343.  
  344.  
  345.       TTTThhhheeee OOOOUUUUTTTTPPPPUUUUTTTT:::: KKKKeeeeyyyywwwwoooorrrrdddd
  346.  
  347.       The OUTPUT: keyword indicates    that certain function
  348.       parameters should be updated (new values made    visible    to
  349.       Perl)    when the XSUB terminates or that certain values    should
  350.       be returned to the calling Perl function.  For simple
  351.       functions, such as the _s_i_n() function    above, the RETVAL
  352.       variable is automatically designated as an output value.  In
  353.       more complex functions the xxxxssssuuuubbbbpppppppp compiler will need help to
  354.       determine which variables are    output variables.
  355.  
  356.       This keyword will normally be    used to    complement the CODE:
  357.       keyword.  The    RETVAL variable    is not recognized as an    output
  358.       variable when    the CODE: keyword is present.  The OUTPUT:
  359.       keyword is used in this situation to tell the    compiler that
  360.       RETVAL really    is an output variable.
  361.  
  362.       The OUTPUT: keyword can also be used to indicate that
  363.       function parameters are output variables.  This may be
  364.       necessary when a parameter has been modified within the
  365.       function and the programmer would like the update to be seen
  366.       by Perl.
  367.  
  368.            bool_t
  369.            rpcb_gettime(host,timep)
  370.             char *host
  371.             time_t &timep
  372.             OUTPUT:
  373.             timep
  374.  
  375.       The OUTPUT: keyword will also    allow an output    parameter to
  376.       be mapped to a matching piece    of code    rather than to a
  377.       typemap.
  378.  
  379.            bool_t
  380.            rpcb_gettime(host,timep)
  381.             char *host
  382.             time_t &timep
  383.             OUTPUT:
  384.             timep sv_setnv(ST(1), (double)timep);
  385.  
  386.       xxxxssssuuuubbbbpppppppp emits an automatic SvSETMAGIC() for all parameters in
  387.       the OUTPUT section of    the XSUB, except RETVAL.  This is the
  388.       usually desired behavior, as it takes    care of    properly
  389.       invoking 'set' magic on output parameters (needed for    hash
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  401.  
  402.  
  403.  
  404.       or array element parameters that must    be created if they
  405.       didn't exist).  If for some reason, this behavior is not
  406.       desired, the OUTPUT section may contain a SETMAGIC: DISABLE
  407.       line to disable it for the remainder of the parameters in
  408.       the OUTPUT section.  Likewise,  SETMAGIC: ENABLE can be used
  409.       to reenable it for the remainder of the OUTPUT section.  See
  410.       the _p_e_r_l_g_u_t_s manpage for more    details    about 'set' magic.
  411.  
  412.       TTTThhhheeee CCCCOOOODDDDEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  413.  
  414.       This keyword is used in more complicated XSUBs which require
  415.       special handling for the C function.    The RETVAL variable is
  416.       available but    will not be returned unless it is specified
  417.       under    the OUTPUT: keyword.
  418.  
  419.       The following    XSUB is    for a C    function which requires
  420.       special handling of its parameters.  The Perl    usage is given
  421.       first.
  422.  
  423.            $status = rpcb_gettime( "localhost", $timep );
  424.  
  425.       The XSUB follows.
  426.  
  427.            bool_t
  428.            rpcb_gettime(host,timep)
  429.             char *host
  430.             time_t timep
  431.             CODE:
  432.              RETVAL    = rpcb_gettime(    host, &timep );
  433.             OUTPUT:
  434.             timep
  435.             RETVAL
  436.  
  437.  
  438.       TTTThhhheeee IIIINNNNIIIITTTT:::: KKKKeeeeyyyywwwwoooorrrrdddd
  439.  
  440.       The INIT: keyword allows initialization to be    inserted into
  441.       the XSUB before the compiler generates the call to the C
  442.       function.  Unlike the    CODE: keyword above, this keyword does
  443.       not affect the way the compiler handles RETVAL.
  444.  
  445.           bool_t
  446.           rpcb_gettime(host,timep)
  447.             char *host
  448.             time_t &timep
  449.             INIT:
  450.             printf("# Host is %s\n", host );
  451.             OUTPUT:
  452.             timep
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.      Page 7                        (printed 10/23/98)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  467.  
  468.  
  469.  
  470.       TTTThhhheeee NNNNOOOO____IIIINNNNIIIITTTT KKKKeeeeyyyywwwwoooorrrrdddd
  471.  
  472.       The NO_INIT keyword is used to indicate that a function
  473.       parameter is being used only as an output value.  The    xxxxssssuuuubbbbpppppppp
  474.       compiler will    normally generate code to read the values of
  475.       all function parameters from the argument stack and assign
  476.       them to C variables upon entry to the    function.  NO_INIT
  477.       will tell the    compiler that some parameters will be used for
  478.       output rather    than for input and that    they will be handled
  479.       before the function terminates.
  480.  
  481.       The following    example    shows a    variation of the
  482.       _r_p_c_b__g_e_t_t_i_m_e() function.  This function uses the timep
  483.       variable only    as an output variable and does not care    about
  484.       its initial contents.
  485.  
  486.            bool_t
  487.            rpcb_gettime(host,timep)
  488.             char *host
  489.             time_t &timep = NO_INIT
  490.             OUTPUT:
  491.             timep
  492.  
  493.  
  494.       IIIInnnniiiittttiiiiaaaalllliiiizzzziiiinnnngggg FFFFuuuunnnnccccttttiiiioooonnnn    PPPPaaaarrrraaaammmmeeeetttteeeerrrrssss
  495.  
  496.       Function parameters are normally initialized with their
  497.       values from the argument stack.  The typemaps    contain    the
  498.       code segments    which are used to transfer the Perl values to
  499.       the C    parameters.  The programmer, however, is allowed to
  500.       override the typemaps    and supply alternate (or additional)
  501.       initialization code.
  502.  
  503.       The following    code demonstrates how to supply    initialization
  504.       code for function parameters.     The initialization code is
  505.       eval'd within    double quotes by the compiler before it    is
  506.       added    to the output so anything which    should be interpreted
  507.       literally [mainly $, @, or \\] must be protected with
  508.       backslashes.    The variables $var, $arg, and $type can    be
  509.       used as in typemaps.
  510.  
  511.            bool_t
  512.            rpcb_gettime(host,timep)
  513.             char *host = (char *)SvPV($arg,PL_na);
  514.             time_t &timep = 0;
  515.             OUTPUT:
  516.             timep
  517.  
  518.       This should not be used to supply default values for
  519.       parameters.  One would normally use this when    a function
  520.       parameter must be processed by another library function
  521.       before it can    be used.  Default parameters are covered in
  522.  
  523.  
  524.  
  525.      Page 8                        (printed 10/23/98)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  533.  
  534.  
  535.  
  536.       the next section.
  537.  
  538.       If the initialization    begins with =, then it is output on
  539.       the same line    where the input    variable is declared.  If the
  540.       initialization begins    with ; or +, then it is    output after
  541.       all of the input variables have been declared.  The =    and ;
  542.       cases    replace    the initialization normally supplied from the
  543.       typemap.  For    the + case, the    initialization from the
  544.       typemap will preceed the initialization code included    after
  545.       the +.  A global variable, %v, is available for the truely
  546.       rare case where information from one initialization is
  547.       needed in another initialization.
  548.  
  549.            bool_t
  550.            rpcb_gettime(host,timep)
  551.             time_t &timep ; /*\$v{time}=@{[$v{time}=$arg]}*/
  552.             char *host + SvOK($v{time})    ? SvPV($arg,PL_na) : NULL;
  553.             OUTPUT:
  554.             timep
  555.  
  556.  
  557.       DDDDeeeeffffaaaauuuulllltttt PPPPaaaarrrraaaammmmeeeetttteeeerrrr VVVVaaaalllluuuueeeessss
  558.  
  559.       Default values can be    specified for function parameters by
  560.       placing an assignment    statement in the parameter list.  The
  561.       default value    may be a number    or a string.  Defaults should
  562.       always be used on the    right-most parameters only.
  563.  
  564.       To allow the XSUB for    _r_p_c_b__g_e_t_t_i_m_e() to have a default host
  565.       value    the parameters to the XSUB could be rearranged.     The
  566.       XSUB will then call the real _r_p_c_b__g_e_t_t_i_m_e() function with
  567.       the parameters in the    correct    order.    Perl will call this
  568.       XSUB with either of the following statements.
  569.  
  570.            $status = rpcb_gettime( $timep, $host );
  571.  
  572.            $status = rpcb_gettime( $timep );
  573.  
  574.       The XSUB will    look like the code  which  follows.   A     CODE:
  575.       block     is used to call the real _r_p_c_b__g_e_t_t_i_m_e() function with
  576.       the parameters in the    correct    order for that function.
  577.  
  578.            bool_t
  579.            rpcb_gettime(timep,host="localhost")
  580.             char *host
  581.             time_t timep = NO_INIT
  582.             CODE:
  583.              RETVAL    = rpcb_gettime(    host, &timep );
  584.             OUTPUT:
  585.             timep
  586.             RETVAL
  587.  
  588.  
  589.  
  590.  
  591.      Page 9                        (printed 10/23/98)
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  599.  
  600.  
  601.  
  602.       TTTThhhheeee PPPPRRRREEEEIIIINNNNIIIITTTT:::: KKKKeeeeyyyywwwwoooorrrrdddd
  603.  
  604.       The PREINIT: keyword allows extra variables to be declared
  605.       before the typemaps are expanded.  If    a variable is declared
  606.       in a CODE: block then    that variable will follow any typemap
  607.       code.     This may result in a C    syntax error.  To force    the
  608.       variable to be declared before the typemap code, place it
  609.       into a PREINIT: block.  The PREINIT: keyword may be used one
  610.       or more times    within an XSUB.
  611.  
  612.       The following    examples are equivalent, but if    the code is
  613.       using    complex    typemaps then the first    example    is safer.
  614.  
  615.            bool_t
  616.            rpcb_gettime(timep)
  617.             time_t timep = NO_INIT
  618.             PREINIT:
  619.             char *host = "localhost";
  620.             CODE:
  621.             RETVAL = rpcb_gettime( host, &timep    );
  622.             OUTPUT:
  623.             timep
  624.             RETVAL
  625.  
  626.       A correct, but error-prone example.
  627.  
  628.            bool_t
  629.            rpcb_gettime(timep)
  630.             time_t timep = NO_INIT
  631.             CODE:
  632.             char *host = "localhost";
  633.             RETVAL = rpcb_gettime( host, &timep    );
  634.             OUTPUT:
  635.             timep
  636.             RETVAL
  637.  
  638.  
  639.       TTTThhhheeee SSSSCCCCOOOOPPPPEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  640.  
  641.       The SCOPE: keyword allows scoping to be enabled for a
  642.       particular XSUB. If enabled, the XSUB    will invoke ENTER and
  643.       LEAVE    automatically.
  644.  
  645.       To support potentially complex type mappings,    if a typemap
  646.       entry    used by    this XSUB contains a comment like /*scope*/
  647.       then scoping will automatically be enabled for that XSUB.
  648.  
  649.       To enable scoping:
  650.  
  651.           SCOPE: ENABLE
  652.  
  653.       To disable scoping:
  654.  
  655.  
  656.  
  657.      Page 10                        (printed 10/23/98)
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  665.  
  666.  
  667.  
  668.           SCOPE: DISABLE
  669.  
  670.  
  671.       TTTThhhheeee IIIINNNNPPPPUUUUTTTT:::: KKKKeeeeyyyywwwwoooorrrrdddd
  672.  
  673.       The XSUB's parameters    are usually evaluated immediately
  674.       after    entering the XSUB.  The    INPUT: keyword can be used to
  675.       force    those parameters to be evaluated a little later.  The
  676.       INPUT: keyword can be    used multiple times within an XSUB and
  677.       can be used to list one or more input    variables.  This
  678.       keyword is used with the PREINIT: keyword.
  679.  
  680.       The following    example    shows how the input parameter timep
  681.       can be evaluated late, after a PREINIT.
  682.  
  683.           bool_t
  684.           rpcb_gettime(host,timep)
  685.             char *host
  686.             PREINIT:
  687.             time_t tt;
  688.             INPUT:
  689.             time_t timep
  690.             CODE:
  691.              RETVAL    = rpcb_gettime(    host, &tt );
  692.              timep = tt;
  693.             OUTPUT:
  694.             timep
  695.             RETVAL
  696.  
  697.       The next example shows each input parameter evaluated    late.
  698.  
  699.           bool_t
  700.           rpcb_gettime(host,timep)
  701.             PREINIT:
  702.             time_t tt;
  703.             INPUT:
  704.             char *host
  705.             PREINIT:
  706.             char *h;
  707.             INPUT:
  708.             time_t timep
  709.             CODE:
  710.              h = host;
  711.              RETVAL    = rpcb_gettime(    h, &tt );
  712.              timep = tt;
  713.             OUTPUT:
  714.             timep
  715.             RETVAL
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.      Page 11                        (printed 10/23/98)
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  731.  
  732.  
  733.  
  734.       VVVVaaaarrrriiiiaaaabbbblllleeee----lllleeeennnnggggtttthhhh PPPPaaaarrrraaaammmmeeeetttteeeerrrr LLLLiiiissssttttssss
  735.  
  736.       XSUBs    can have variable-length parameter lists by specifying
  737.       an ellipsis (...) in the parameter list.  This use of    the
  738.       ellipsis is similar to that found in ANSI C.    The programmer
  739.       is able to determine the number of arguments passed to the
  740.       XSUB by examining the    items variable which the xxxxssssuuuubbbbpppppppp
  741.       compiler supplies for    all XSUBs.  By using this mechanism
  742.       one can create an XSUB which accepts a list of parameters of
  743.       unknown length.
  744.  
  745.       The _h_o_s_t parameter for the _r_p_c_b__g_e_t_t_i_m_e() XSUB can be
  746.       optional so the ellipsis can be used to indicate that    the
  747.       XSUB will take a variable number of parameters.  Perl    should
  748.       be able to call this XSUB with either    of the following
  749.       statements.
  750.  
  751.            $status = rpcb_gettime( $timep, $host );
  752.  
  753.            $status = rpcb_gettime( $timep );
  754.  
  755.       The XS code, with ellipsis, follows.
  756.  
  757.            bool_t
  758.            rpcb_gettime(timep, ...)
  759.             time_t timep = NO_INIT
  760.             PREINIT:
  761.             char *host = "localhost";
  762.             CODE:
  763.                 if(    items >    1 )
  764.                  host =    (char *)SvPV(ST(1), PL_na);
  765.                 RETVAL = rpcb_gettime( host, &timep    );
  766.             OUTPUT:
  767.             timep
  768.             RETVAL
  769.  
  770.  
  771.       TTTThhhheeee CCCC____AAAARRRRGGGGSSSS:::: KKKKeeeeyyyywwwwoooorrrrdddd
  772.  
  773.       The C_ARGS: keyword allows creating of XSUBS which have
  774.       different calling sequence from Perl than from C, without a
  775.       need to write    CODE: or CPPCODE: section.  The    contents of
  776.       the C_ARGS: paragraph    is put as the argument to the called C
  777.       function without any change.
  778.  
  779.       For example, suppose that C function is declared as
  780.  
  781.           symbolic nth_derivative(int n, symbolic function,    int flags);
  782.  
  783.       and that the default flags are kept in a global C variable
  784.       default_flags.  Suppose that you want    to create an interface
  785.       which    is called as
  786.  
  787.  
  788.  
  789.      Page 12                        (printed 10/23/98)
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  797.  
  798.  
  799.  
  800.           $second_deriv = $function->nth_derivative(2);
  801.  
  802.       To do    this, declare the XSUB as
  803.  
  804.           symbolic
  805.           nth_derivative(function, n)
  806.           symbolic      function
  807.           int          n
  808.           C_ARGS:
  809.           n, function, default_flags
  810.  
  811.  
  812.       TTTThhhheeee PPPPPPPPCCCCOOOODDDDEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  813.  
  814.       The PPCODE: keyword is an alternate form of the CODE:
  815.       keyword and is used to tell the xxxxssssuuuubbbbpppppppp compiler that the
  816.       programmer is    supplying the code to control the argument
  817.       stack    for the    XSUBs return values.  Occasionally one will
  818.       want an XSUB to return a list    of values rather than a    single
  819.       value.  In these cases one must use PPCODE: and then
  820.       explicitly push the list of values on    the stack.  The
  821.       PPCODE: and CODE:  keywords are not used together within the
  822.       same XSUB.
  823.  
  824.       The following    XSUB will call the C _r_p_c_b__g_e_t_t_i_m_e() function
  825.       and will return its two output values, timep and status, to
  826.       Perl as a single list.
  827.  
  828.            void
  829.            rpcb_gettime(host)
  830.             char *host
  831.             PREINIT:
  832.             time_t  timep;
  833.             bool_t  status;
  834.             PPCODE:
  835.             status = rpcb_gettime( host, &timep    );
  836.             EXTEND(SP, 2);
  837.             PUSHs(sv_2mortal(newSViv(status)));
  838.             PUSHs(sv_2mortal(newSViv(timep)));
  839.  
  840.       Notice that the programmer must supply the C code necessary
  841.       to have the real _r_p_c_b__g_e_t_t_i_m_e() function called and to have
  842.       the return values properly placed on the argument stack.
  843.  
  844.       The void return type for this    function tells the xxxxssssuuuubbbbpppppppp
  845.       compiler that    the RETVAL variable is not needed or used and
  846.       that it should not be    created.  In most scenarios the    void
  847.       return type should be    used with the PPCODE:  directive.
  848.  
  849.       The _E_X_T_E_N_D() macro is    used to    make room on the argument
  850.       stack    for 2 return values.  The PPCODE: directive causes the
  851.       xxxxssssuuuubbbbpppppppp compiler to create a stack pointer available as SP,
  852.  
  853.  
  854.  
  855.      Page 13                        (printed 10/23/98)
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  863.  
  864.  
  865.  
  866.       and it is this pointer which is being    used in    the _E_X_T_E_N_D()
  867.       macro.  The values are then pushed onto the stack with the
  868.       _P_U_S_H_s() macro.
  869.  
  870.       Now the _r_p_c_b__g_e_t_t_i_m_e() function can be used from Perl    with
  871.       the following    statement.
  872.  
  873.            ($status, $timep) = rpcb_gettime("localhost");
  874.  
  875.       When handling    output parameters with a PPCODE    section, be
  876.       sure to handle 'set' magic properly.    See the    _p_e_r_l_g_u_t_s
  877.       manpage for details about 'set' magic.
  878.  
  879.       RRRReeeettttuuuurrrrnnnniiiinnnngggg UUUUnnnnddddeeeeffff AAAAnnnndddd EEEEmmmmppppttttyyyy LLLLiiiissssttttssss
  880.  
  881.       Occasionally the programmer will want    to return simply undef
  882.       or an    empty list if a    function fails rather than a separate
  883.       status value.     The _r_p_c_b__g_e_t_t_i_m_e() function offers just this
  884.       situation.  If the function succeeds we would    like to    have
  885.       it return the    time and if it fails we    would like to have
  886.       undef    returned.  In the following Perl code the value    of
  887.       $timep will either be    undef or it will be a valid time.
  888.  
  889.            $timep =    rpcb_gettime( "localhost" );
  890.  
  891.       The following    XSUB uses the SV * return type as a mnemonic
  892.       only,    and uses a CODE: block to indicate to the compiler
  893.       that the programmer has supplied all the necessary code.
  894.       The _s_v__n_e_w_m_o_r_t_a_l() call will initialize the return value to
  895.       undef, making    that the default return    value.
  896.  
  897.            SV *
  898.            rpcb_gettime(host)
  899.             char *  host
  900.             PREINIT:
  901.             time_t  timep;
  902.             bool_t x;
  903.             CODE:
  904.             ST(0) = sv_newmortal();
  905.             if(    rpcb_gettime( host, &timep ) )
  906.              sv_setnv( ST(0), (double)timep);
  907.  
  908.       The next example demonstrates    how one    would place an
  909.       explicit undef in the    return value, should the need arise.
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.      Page 14                        (printed 10/23/98)
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  929.  
  930.  
  931.  
  932.            SV *
  933.            rpcb_gettime(host)
  934.             char *  host
  935.             PREINIT:
  936.             time_t  timep;
  937.             bool_t x;
  938.             CODE:
  939.             ST(0) = sv_newmortal();
  940.             if(    rpcb_gettime( host, &timep ) ){
  941.              sv_setnv( ST(0), (double)timep);
  942.             }
  943.             else{
  944.              ST(0) = &PL_sv_undef;
  945.             }
  946.  
  947.       To return an empty list one must use a PPCODE: block and
  948.       then not push    return values on the stack.
  949.  
  950.            void
  951.            rpcb_gettime(host)
  952.             char *host
  953.             PREINIT:
  954.             time_t  timep;
  955.             PPCODE:
  956.             if(    rpcb_gettime( host, &timep ) )
  957.              PUSHs(sv_2mortal(newSViv(timep)));
  958.             else{
  959.             /* Nothing pushed on stack,    so an empty */
  960.             /* list is implicitly returned. */
  961.             }
  962.  
  963.       Some people may be inclined to include an explicit return in
  964.       the above XSUB, rather than letting control fall through to
  965.       the end.  In those situations    XSRETURN_EMPTY should be used,
  966.       instead.  This will ensure that the XSUB stack is properly
  967.       adjusted.  Consult the section on _A_P_I    _L_I_S_T_I_N_G    in the
  968.       _p_e_r_l_g_u_t_s manpage for other XSRETURN macros.
  969.  
  970.       TTTThhhheeee RRRREEEEQQQQUUUUIIIIRRRREEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  971.  
  972.       The REQUIRE: keyword is used to indicate the minimum version
  973.       of the xxxxssssuuuubbbbpppppppp    compiler needed    to compile the XS module.  An
  974.       XS module which contains the following statement will
  975.       compile with only xxxxssssuuuubbbbpppppppp version 1.922 or greater:
  976.  
  977.           REQUIRE: 1.922
  978.  
  979.  
  980.       TTTThhhheeee CCCCLLLLEEEEAAAANNNNUUUUPPPP:::: KKKKeeeeyyyywwwwoooorrrrdddd
  981.  
  982.       This keyword can be used when    an XSUB    requires special
  983.       cleanup procedures before it terminates.  When the CLEANUP:
  984.  
  985.  
  986.  
  987.      Page 15                        (printed 10/23/98)
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  995.  
  996.  
  997.  
  998.       keyword is used it must follow any CODE:, PPCODE:, or
  999.       OUTPUT: blocks which are present in the XSUB.     The code
  1000.       specified for    the cleanup block will be added    as the last
  1001.       statements in    the XSUB.
  1002.  
  1003.       TTTThhhheeee BBBBOOOOOOOOTTTT:::: KKKKeeeeyyyywwwwoooorrrrdddd
  1004.  
  1005.       The BOOT: keyword is used to add code    to the extension's
  1006.       bootstrap function.  The bootstrap function is generated by
  1007.       the xxxxssssuuuubbbbpppppppp compiler and normally holds the statements
  1008.       necessary to register    any XSUBs with Perl.  With the BOOT:
  1009.       keyword the programmer can tell the compiler to add extra
  1010.       statements to    the bootstrap function.
  1011.  
  1012.       This keyword may be used any time after the first MODULE
  1013.       keyword and should appear on a line by itself.  The first
  1014.       blank    line after the keyword will terminate the code block.
  1015.  
  1016.            BOOT:
  1017.            # The following message will be printed when the
  1018.            # bootstrap function executes.
  1019.            printf("Hello from the bootstrap!\n");
  1020.  
  1021.  
  1022.       TTTThhhheeee VVVVEEEERRRRSSSSIIIIOOOONNNNCCCCHHHHEEEECCCCKKKK:::: KKKKeeeeyyyywwwwoooorrrrdddd
  1023.  
  1024.       The VERSIONCHECK: keyword corresponds    to xxxxssssuuuubbbbpppppppp's
  1025.       -versioncheck    and -noversioncheck options.  This keyword
  1026.       overrides the    command    line options.  Version checking    is
  1027.       enabled by default.  When version checking is    enabled    the XS
  1028.       module will attempt to verify    that its version matches the
  1029.       version of the PM module.
  1030.  
  1031.       To enable version checking:
  1032.  
  1033.           VERSIONCHECK: ENABLE
  1034.  
  1035.       To disable version checking:
  1036.  
  1037.           VERSIONCHECK: DISABLE
  1038.  
  1039.  
  1040.       TTTThhhheeee PPPPRRRROOOOTTTTOOOOTTTTYYYYPPPPEEEESSSS:::: KKKKeeeeyyyywwwwoooorrrrdddd
  1041.  
  1042.       The PROTOTYPES: keyword corresponds to xxxxssssuuuubbbbpppppppp's -prototypes
  1043.       and -noprototypes options.  This keyword overrides the
  1044.       command line options.     Prototypes are    enabled    by default.
  1045.       When prototypes are enabled XSUBs will be given Perl
  1046.       prototypes.  This keyword may    be used    multiple times in an
  1047.       XS module to enable and disable prototypes for different
  1048.       parts    of the module.
  1049.  
  1050.  
  1051.  
  1052.  
  1053.      Page 16                        (printed 10/23/98)
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1061.  
  1062.  
  1063.  
  1064.       To enable prototypes:
  1065.  
  1066.           PROTOTYPES: ENABLE
  1067.  
  1068.       To disable prototypes:
  1069.  
  1070.           PROTOTYPES: DISABLE
  1071.  
  1072.  
  1073.       TTTThhhheeee PPPPRRRROOOOTTTTOOOOTTTTYYYYPPPPEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  1074.  
  1075.       This keyword is similar to the PROTOTYPES: keyword above but
  1076.       can be used to force xxxxssssuuuubbbbpppppppp to use a specific    prototype for
  1077.       the XSUB.  This keyword overrides all    other prototype
  1078.       options and keywords but affects only    the current XSUB.
  1079.       Consult the Prototypes entry in the _p_e_r_l_s_u_b manpage for
  1080.       information about Perl prototypes.
  1081.  
  1082.           bool_t
  1083.           rpcb_gettime(timep, ...)
  1084.             time_t timep = NO_INIT
  1085.             PROTOTYPE: $;$
  1086.             PREINIT:
  1087.             char *host = "localhost";
  1088.             CODE:
  1089.                 if(    items >    1 )
  1090.                  host =    (char *)SvPV(ST(1), PL_na);
  1091.                 RETVAL = rpcb_gettime( host, &timep    );
  1092.             OUTPUT:
  1093.             timep
  1094.             RETVAL
  1095.  
  1096.  
  1097.       TTTThhhheeee AAAALLLLIIIIAAAASSSS:::: KKKKeeeeyyyywwwwoooorrrrdddd
  1098.  
  1099.       The ALIAS: keyword allows an XSUB to have two    or more    unique
  1100.       Perl names and to know which of those    names was used when it
  1101.       was invoked.    The Perl names may be fully-qualified with
  1102.       package names.  Each alias is    given an index.     The compiler
  1103.       will setup a variable    called ix which    contain    the index of
  1104.       the alias which was used.  When the XSUB is called with its
  1105.       declared name    ix will    be 0.
  1106.  
  1107.       The following    example    will create aliases FOO::gettime() and
  1108.       BAR::getit() for this    function.
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.      Page 17                        (printed 10/23/98)
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1127.  
  1128.  
  1129.  
  1130.           bool_t
  1131.           rpcb_gettime(host,timep)
  1132.             char *host
  1133.             time_t &timep
  1134.             ALIAS:
  1135.               FOO::gettime = 1
  1136.               BAR::getit = 2
  1137.             INIT:
  1138.             printf("# ix = %d\n", ix );
  1139.             OUTPUT:
  1140.             timep
  1141.  
  1142.  
  1143.       TTTThhhheeee IIIINNNNTTTTEEEERRRRFFFFAAAACCCCEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  1144.  
  1145.       This keyword declares    the current XSUB as a keeper of    the
  1146.       given    calling    signature.  If some text follows this keyword,
  1147.       it is    considered as a    list of    functions which    have this
  1148.       signature, and should    be attached to XSUBs.
  1149.  
  1150.       Say, if you have 4 functions _m_u_l_t_i_p_l_y(), _d_i_v_i_d_e(), _a_d_d(),
  1151.       _s_u_b_t_r_a_c_t() all having    the signature
  1152.  
  1153.           symbolic f(symbolic, symbolic);
  1154.  
  1155.       you code them    all by using XSUB
  1156.  
  1157.           symbolic
  1158.           interface_s_ss(arg1, arg2)
  1159.           symbolic      arg1
  1160.           symbolic      arg2
  1161.           INTERFACE:
  1162.           multiply divide
  1163.           add subtract
  1164.  
  1165.       The advantage    of this    approach comparing to ALIAS: keyword
  1166.       is that one can attach an extra function _r_e_m_a_i_n_d_e_r() at
  1167.       runtime by using
  1168.  
  1169.           CV *mycv = _n_e_w_X_S_p_r_o_t_o("Symbolic::remainder",
  1170.                XS_Symbolic_interface_s_ss, __FILE__,
  1171.       "$$");
  1172.           _X_S_I_N_T_E_R_F_A_C_E__F_U_N_C__S_E_T(mycv, remainder);
  1173.  
  1174.       (This    example    supposes that there was    no INTERFACE_MACRO:
  1175.       section, otherwise one needs to use something    else instead
  1176.       of XSINTERFACE_FUNC_SET.)
  1177.  
  1178.       TTTThhhheeee IIIINNNNTTTTEEEERRRRFFFFAAAACCCCEEEE____MMMMAAAACCCCRRRROOOO:::: KKKKeeeeyyyywwwwoooorrrrdddd
  1179.  
  1180.       This keyword allows one to define an INTERFACE using a
  1181.       different way    to extract a function pointer from an XSUB.
  1182.  
  1183.  
  1184.  
  1185.      Page 18                        (printed 10/23/98)
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1193.  
  1194.  
  1195.  
  1196.       The text which follows this keyword should give the name of
  1197.       macros which would extract/set a function pointer.  The
  1198.       extractor macro is given return type,    CV*, and
  1199.       XSANY.any_dptr for this CV*.    The setter macro is given cv,
  1200.       and the function pointer.
  1201.  
  1202.       The default value is XSINTERFACE_FUNC    and
  1203.       XSINTERFACE_FUNC_SET.     An INTERFACE keyword with an empty
  1204.       list of functions can    be omitted if INTERFACE_MACRO keyword
  1205.       is used.
  1206.  
  1207.       Suppose that in the previous example functions pointers for
  1208.       _m_u_l_t_i_p_l_y(), _d_i_v_i_d_e(),    _a_d_d(), _s_u_b_t_r_a_c_t() are kept in a    global
  1209.       C array fp[] with offsets being multiply_off,    divide_off,
  1210.       add_off, subtract_off.  Then one can use
  1211.  
  1212.           #define XSINTERFACE_FUNC_BYOFFSET(ret,cv,f) \
  1213.           ((XSINTERFACE_CVT(ret,))fp[CvXSUBANY(cv).any_i32])
  1214.           #define XSINTERFACE_FUNC_BYOFFSET_set(cv,f) \
  1215.           CvXSUBANY(cv).any_i32    = CAT2(    f, _off    )
  1216.  
  1217.       in C section,
  1218.  
  1219.           symbolic
  1220.           interface_s_ss(arg1, arg2)
  1221.           symbolic      arg1
  1222.           symbolic      arg2
  1223.           INTERFACE_MACRO:
  1224.           XSINTERFACE_FUNC_BYOFFSET
  1225.           XSINTERFACE_FUNC_BYOFFSET_set
  1226.           INTERFACE:
  1227.           multiply divide
  1228.           add subtract
  1229.  
  1230.       in XSUB section.
  1231.  
  1232.       TTTThhhheeee IIIINNNNCCCCLLLLUUUUDDDDEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  1233.  
  1234.       This keyword can be used to pull other files into the    XS
  1235.       module.  The other files may have XS code.  INCLUDE: can
  1236.       also be used to run a    command    to generate the    XS code    to be
  1237.       pulled into the module.
  1238.  
  1239.       The file _R_p_c_b_1._x_s_h contains our rpcb_gettime() function:
  1240.  
  1241.           bool_t
  1242.           rpcb_gettime(host,timep)
  1243.             char *host
  1244.             time_t &timep
  1245.             OUTPUT:
  1246.             timep
  1247.  
  1248.  
  1249.  
  1250.  
  1251.      Page 19                        (printed 10/23/98)
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1259.  
  1260.  
  1261.  
  1262.       The XS module    can use    INCLUDE: to pull that file into    it.
  1263.  
  1264.           INCLUDE: Rpcb1.xsh
  1265.  
  1266.       If the parameters to the INCLUDE: keyword are    followed by a
  1267.       pipe (|) then    the compiler will interpret the    parameters as
  1268.       a command.
  1269.  
  1270.           INCLUDE: cat Rpcb1.xsh |
  1271.  
  1272.  
  1273.       TTTThhhheeee CCCCAAAASSSSEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  1274.  
  1275.       The CASE: keyword allows an XSUB to have multiple distinct
  1276.       parts    with each part acting as a virtual XSUB.  CASE:    is
  1277.       greedy and if    it is used then    all other XS keywords must be
  1278.       contained within a CASE:.  This means    nothing    may precede
  1279.       the first CASE: in the XSUB and anything following the last
  1280.       CASE:    is included in that case.
  1281.  
  1282.       A CASE: might    switch via a parameter of the XSUB, via    the ix
  1283.       ALIAS:  variable (see    the section on _T_h_e _A_L_I_A_S: _K_e_y_w_o_r_d), or
  1284.       maybe    via the    items variable (see the    section    on _V_a_r_i_a_b_l_e-
  1285.       _l_e_n_g_t_h _P_a_r_a_m_e_t_e_r _L_i_s_t_s).  The    last CASE: becomes the ddddeeeeffffaaaauuuulllltttt
  1286.       case if it is    not associated with a conditional.  The
  1287.       following example shows CASE switched    via ix with a function
  1288.       rpcb_gettime() having    an alias x_gettime().  When the
  1289.       function is called as    rpcb_gettime() its parameters are the
  1290.       usual    (char *host, time_t *timep), but when the function is
  1291.       called as x_gettime()    its parameters are reversed, (time_t
  1292.       *timep, char *host).
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.      Page 20                        (printed 10/23/98)
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1325.  
  1326.  
  1327.  
  1328.           long
  1329.           rpcb_gettime(a,b)
  1330.         CASE: ix == 1
  1331.             ALIAS:
  1332.             x_gettime =    1
  1333.             INPUT:
  1334.             # 'a' is timep, 'b'    is host
  1335.             char *b
  1336.             time_t a = NO_INIT
  1337.             CODE:
  1338.              RETVAL    = rpcb_gettime(    b, &a );
  1339.             OUTPUT:
  1340.             a
  1341.             RETVAL
  1342.         CASE:
  1343.             # 'a' is host, 'b' is timep
  1344.             char *a
  1345.             time_t &b =    NO_INIT
  1346.             OUTPUT:
  1347.             b
  1348.             RETVAL
  1349.  
  1350.       That function    can be called with either of the following
  1351.       statements.  Note the    different argument lists.
  1352.  
  1353.           $status = rpcb_gettime( $host, $timep    );
  1354.  
  1355.           $status = x_gettime( $timep, $host );
  1356.  
  1357.  
  1358.       TTTThhhheeee &&&&    UUUUnnnnaaaarrrryyyy OOOOppppeeeerrrraaaattttoooorrrr
  1359.  
  1360.       The &    unary operator is used to tell the compiler that it
  1361.       should dereference the object    when it    calls the C function.
  1362.       This is used when a CODE: block is not used and the object
  1363.       is a not a pointer type (the object is an int    or long    but
  1364.       not a    int* or    long*).
  1365.  
  1366.       The following    XSUB will generate incorrect C code.  The
  1367.       xsubpp compiler will turn this into code which calls
  1368.       rpcb_gettime() with parameters (char *host, time_t timep),
  1369.       but the real rpcb_gettime() wants the    timep parameter    to be
  1370.       of type time_t* rather than time_t.
  1371.  
  1372.           bool_t
  1373.           rpcb_gettime(host,timep)
  1374.             char *host
  1375.             time_t timep
  1376.             OUTPUT:
  1377.             timep
  1378.  
  1379.       That problem is corrected by using the & operator.  The
  1380.  
  1381.  
  1382.  
  1383.      Page 21                        (printed 10/23/98)
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1391.  
  1392.  
  1393.  
  1394.       xsubpp compiler will now turn    this into code which calls
  1395.       rpcb_gettime() correctly with    parameters (char *host,    time_t
  1396.       *timep).  It does this by carrying the & through, so the
  1397.       function call    looks like rpcb_gettime(host, &timep).
  1398.  
  1399.           bool_t
  1400.           rpcb_gettime(host,timep)
  1401.             char *host
  1402.             time_t &timep
  1403.             OUTPUT:
  1404.             timep
  1405.  
  1406.  
  1407.       IIIInnnnsssseeeerrrrttttiiiinnnngggg CCCCoooommmmmmmmeeeennnnttttssss aaaannnndddd CCCC PPPPrrrreeeepppprrrroooocccceeeessssssssoooorrrr    DDDDiiiirrrreeeeccccttttiiiivvvveeeessss
  1408.  
  1409.       C preprocessor directives are    allowed    within BOOT:, PREINIT:
  1410.       INIT:, CODE:,    PPCODE:, and CLEANUP: blocks, as well as
  1411.       outside the functions.  Comments are allowed anywhere    after
  1412.       the MODULE keyword.  The compiler will pass the preprocessor
  1413.       directives through untouched and will    remove the commented
  1414.       lines.
  1415.  
  1416.       Comments can be added    to XSUBs by placing a #    as the first
  1417.       non-whitespace of a line.  Care should be taken to avoid
  1418.       making the comment look like a C preprocessor    directive,
  1419.       lest it be interpreted as such.  The simplest    way to prevent
  1420.       this is to put whitespace in front of    the #.
  1421.  
  1422.       If you use preprocessor directives to    choose one of two
  1423.       versions of a    function, use
  1424.  
  1425.           #if ... version1
  1426.           #else /* ... version2  */
  1427.           #endif
  1428.  
  1429.       and not
  1430.  
  1431.           #if ... version1
  1432.           #endif
  1433.           #if ... version2
  1434.           #endif
  1435.  
  1436.       because otherwise xsubpp will    believe    that you made a
  1437.       duplicate definition of the function.     Also, put a blank
  1438.       line before the #else/#endif so it will not be seen as part
  1439.       of the function body.
  1440.  
  1441.       UUUUssssiiiinnnngggg    XXXXSSSS WWWWiiiitttthhhh    CCCC++++++++
  1442.  
  1443.       If a function    is defined as a    C++ method then    it will    assume
  1444.       its first argument is    an object pointer.  The    object pointer
  1445.       will be stored in a variable called THIS.  The object    should
  1446.  
  1447.  
  1448.  
  1449.      Page 22                        (printed 10/23/98)
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1457.  
  1458.  
  1459.  
  1460.       have been created by C++ with    the _n_e_w() function and should
  1461.       be blessed by    Perl with the _s_v__s_e_t_r_e_f__p_v() macro.  The
  1462.       blessing of the object by Perl can be    handled    by a typemap.
  1463.       An example typemap is    shown at the end of this section.
  1464.  
  1465.       If the method    is defined as static it    will call the C++
  1466.       function using the _c_l_a_s_s::_m_e_t_h_o_d() syntax.  If the method is
  1467.       not static the function will be called using the
  1468.       THIS->_m_e_t_h_o_d() syntax.
  1469.  
  1470.       The next examples will use the following C++ class.
  1471.  
  1472.            class color {
  1473.             public:
  1474.             color();
  1475.             ~color();
  1476.             int    blue();
  1477.             void set_blue( int );
  1478.  
  1479.             private:
  1480.             int    c_blue;
  1481.            };
  1482.  
  1483.       The XSUBs for    the _b_l_u_e() and _s_e_t__b_l_u_e() methods are defined
  1484.       with the class name but the parameter    for the    object (THIS,
  1485.       or "self") is    implicit and is    not listed.
  1486.  
  1487.            int
  1488.            color::blue()
  1489.  
  1490.            void
  1491.            color::set_blue(    val )
  1492.             int    val
  1493.  
  1494.       Both functions will expect an    object as the first parameter.
  1495.       The xsubpp compiler will call    that object THIS and will use
  1496.       it to    call the specified method.  So in the C++ code the
  1497.       _b_l_u_e() and _s_e_t__b_l_u_e()    methods    will be    called in the
  1498.       following manner.
  1499.  
  1500.            RETVAL =    THIS->blue();
  1501.  
  1502.            THIS->set_blue( val );
  1503.  
  1504.       If the function's name is DDDDEEEESSSSTTTTRRRROOOOYYYY then the C++ delete
  1505.       function will    be called and THIS will    be given as its
  1506.       parameter.
  1507.  
  1508.            void
  1509.            color::DESTROY()
  1510.  
  1511.       The C++ code will call delete.
  1512.  
  1513.  
  1514.  
  1515.      Page 23                        (printed 10/23/98)
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1523.  
  1524.  
  1525.  
  1526.            delete THIS;
  1527.  
  1528.       If the function's name is nnnneeeewwww    then the C++ new function will
  1529.       be called to create a    dynamic    C++ object.  The XSUB will
  1530.       expect the class name, which will be kept in a variable
  1531.       called CLASS,    to be given as the first argument.
  1532.  
  1533.            color *
  1534.            color::new()
  1535.  
  1536.       The C++ code will call new.
  1537.  
  1538.           RETVAL = new color();
  1539.  
  1540.       The following    is an example of a typemap that    could be used
  1541.       for this C++ example.
  1542.  
  1543.           TYPEMAP
  1544.           color *          O_OBJECT
  1545.  
  1546.           OUTPUT
  1547.           #    The Perl object    is blessed into    'CLASS', which should be a
  1548.           #    char* having the name of the package for the blessing.
  1549.           O_OBJECT
  1550.           sv_setref_pv(    $arg, CLASS, (void*)$var );
  1551.  
  1552.           INPUT
  1553.           O_OBJECT
  1554.           if( sv_isobject($arg)    && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
  1555.               $var = ($type)SvIV((SV*)SvRV(    $arg ));
  1556.           else{
  1557.               warn(    \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
  1558.               XSRETURN_UNDEF;
  1559.           }
  1560.  
  1561.  
  1562.       IIIInnnntttteeeerrrrffffaaaacccceeee SSSSttttrrrraaaatttteeeeggggyyyy
  1563.  
  1564.       When designing an interface between Perl and a C library a
  1565.       straight translation from C to XS is often sufficient.  The
  1566.       interface will often be very C-like and occasionally
  1567.       nonintuitive,    especially when    the C function modifies    one of
  1568.       its parameters.  In cases where the programmer wishes    to
  1569.       create a more    Perl-like interface the    following strategy may
  1570.       help to identify the more critical parts of the interface.
  1571.  
  1572.       Identify the C functions which modify    their parameters.  The
  1573.       XSUBs    for these functions may    be able    to return lists    to
  1574.       Perl,    or may be candidates to    return undef or    an empty list
  1575.       in case of failure.
  1576.  
  1577.       Identify which values    are used by only the C and XSUB
  1578.  
  1579.  
  1580.  
  1581.      Page 24                        (printed 10/23/98)
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1589.  
  1590.  
  1591.  
  1592.       functions themselves.     If Perl does not need to access the
  1593.       contents of the value    then it    may not    be necessary to
  1594.       provide a translation    for that value from C to Perl.
  1595.  
  1596.       Identify the pointers    in the C function parameter lists and
  1597.       return values.  Some pointers    can be handled in XS with the
  1598.       & unary operator on the variable name    while others will
  1599.       require the use of the * operator on the type    name.  In
  1600.       general it is    easier to work with the    & operator.
  1601.  
  1602.       Identify the structures used by the C    functions.  In many
  1603.       cases    it may be helpful to use the T_PTROBJ typemap for
  1604.       these    structures so they can be manipulated by Perl as
  1605.       blessed objects.
  1606.  
  1607.       PPPPeeeerrrrllll OOOObbbbjjjjeeeeccccttttssss AAAAnnnndddd CCCC SSSSttttrrrruuuuccccttttuuuurrrreeeessss
  1608.  
  1609.       When dealing with C structures one should select either
  1610.       TTTT____PPPPTTTTRRRROOOOBBBBJJJJ or TTTT____PPPPTTTTRRRRRRRREEEEFFFF for the XS type.     Both types are
  1611.       designed to handle pointers to complex objects.  The
  1612.       T_PTRREF type    will allow the Perl object to be unblessed
  1613.       while    the T_PTROBJ type requires that    the object be blessed.
  1614.       By using T_PTROBJ one    can achieve a form of type-checking
  1615.       because the XSUB will    attempt    to verify that the Perl    object
  1616.       is of    the expected type.
  1617.  
  1618.       The following    XS code    shows the _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t() function
  1619.       which    is used    with ONC+ TIRPC.  The _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t()
  1620.       function will    return a pointer to a C    structure and has the
  1621.       C prototype shown below.  The    example    will demonstrate how
  1622.       the C    pointer    will become a Perl reference.  Perl will
  1623.       consider this    reference to be    a pointer to a blessed object
  1624.       and will attempt to call a destructor    for the    object.     A
  1625.       destructor will be provided in the XS    source to free the
  1626.       memory used by _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t().  Destructors in XS can be
  1627.       created by specifying    an XSUB    function whose name ends with
  1628.       the word DDDDEEEESSSSTTTTRRRROOOOYYYY.  XS    destructors can    be used    to free    memory
  1629.       which    may have been malloc'd by another XSUB.
  1630.  
  1631.            struct netconfig    *getnetconfigent(const char *netid);
  1632.  
  1633.       A typedef will be created for    struct netconfig.  The Perl
  1634.       object will be blessed in a class matching the name of the C
  1635.       type,    with the tag Ptr appended, and the name    should not
  1636.       have embedded    spaces if it will be a Perl package name.  The
  1637.       destructor will be placed in a class corresponding to    the
  1638.       class    of the object and the PREFIX keyword will be used to
  1639.       trim the name    to the word DESTROY as Perl will expect.
  1640.  
  1641.            typedef struct netconfig    Netconfig;
  1642.  
  1643.            MODULE =    RPC  PACKAGE = RPC
  1644.  
  1645.  
  1646.  
  1647.      Page 25                        (printed 10/23/98)
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1655.  
  1656.  
  1657.  
  1658.            Netconfig *
  1659.            getnetconfigent(netid)
  1660.             char *netid
  1661.  
  1662.            MODULE =    RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_
  1663.  
  1664.            void
  1665.            rpcb_DESTROY(netconf)
  1666.             Netconfig *netconf
  1667.             CODE:
  1668.             printf("Now    in NetconfigPtr::DESTROY\n");
  1669.             free( netconf );
  1670.  
  1671.       This example requires    the following typemap entry.  Consult
  1672.       the typemap section for more information about adding    new
  1673.       typemaps for an extension.
  1674.  
  1675.            TYPEMAP
  1676.            Netconfig *  T_PTROBJ
  1677.  
  1678.       This example will be used with the following Perl
  1679.       statements.
  1680.  
  1681.            use RPC;
  1682.            $netconf    = getnetconfigent("udp");
  1683.  
  1684.       When Perl destroys the object    referenced by $netconf it will
  1685.       send the object to the supplied XSUB DESTROY function.  Perl
  1686.       cannot determine, and    does not care, that this object    is a C
  1687.       struct and not a Perl    object.     In this sense,    there is no
  1688.       difference between the object    created    by the
  1689.       _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t() XSUB and an    object created by a normal
  1690.       Perl subroutine.
  1691.  
  1692.       TTTThhhheeee TTTTyyyyppppeeeemmmmaaaapppp
  1693.  
  1694.       The typemap is a collection of code fragments    which are used
  1695.       by the xxxxssssuuuubbbbpppppppp    compiler to map    C function parameters and
  1696.       values to Perl values.  The typemap file may consist of
  1697.       three    sections labeled TYPEMAP, INPUT, and OUTPUT.  The
  1698.       INPUT    section    tells the compiler how to translate Perl
  1699.       values into variables    of certain C types.  The OUTPUT
  1700.       section tells    the compiler how to translate the values from
  1701.       certain C types into values Perl can understand.  The
  1702.       TYPEMAP section tells    the compiler which of the INPUT    and
  1703.       OUTPUT code fragments    should be used to map a    given C    type
  1704.       to a Perl value.  Each of the    sections of the    typemap    must
  1705.       be preceded by one of    the TYPEMAP, INPUT, or OUTPUT
  1706.       keywords.
  1707.  
  1708.       The default typemap in the ext directory of the Perl source
  1709.       contains many    useful types which can be used by Perl
  1710.  
  1711.  
  1712.  
  1713.      Page 26                        (printed 10/23/98)
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1721.  
  1722.  
  1723.  
  1724.       extensions.  Some extensions define additional typemaps
  1725.       which    they keep in their own directory.  These additional
  1726.       typemaps may reference INPUT and OUTPUT maps in the main
  1727.       typemap.  The    xxxxssssuuuubbbbpppppppp compiler    will allow the extension's own
  1728.       typemap to override any mappings which are in    the default
  1729.       typemap.
  1730.  
  1731.       Most extensions which    require    a custom typemap will need
  1732.       only the TYPEMAP section of the typemap file.     The custom
  1733.       typemap used in the _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t()    example    shown earlier
  1734.       demonstrates what may    be the typical use of extension
  1735.       typemaps.  That typemap is used to equate a C    structure with
  1736.       the T_PTROBJ typemap.     The typemap used by _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t()
  1737.       is shown here.  Note that the    C type is separated from the
  1738.       XS type with a tab and that the C unary operator * is
  1739.       considered to    be a part of the C type    name.
  1740.  
  1741.            TYPEMAP
  1742.            Netconfig *<tab>T_PTROBJ
  1743.  
  1744.       Here's a more    complicated example: suppose that you wanted
  1745.       struct netconfig to be blessed into the class    Net::Config.
  1746.       One way to do    this is    to use underscores (_) to separate
  1747.       package names, as follows:
  1748.  
  1749.           typedef struct netconfig * Net_Config;
  1750.  
  1751.       And then provide a typemap entry T_PTROBJ_SPECIAL that maps
  1752.       underscores to double-colons (::), and declare Net_Config to
  1753.       be of    that type:
  1754.  
  1755.           TYPEMAP
  1756.           Net_Config      T_PTROBJ_SPECIAL
  1757.  
  1758.           INPUT
  1759.           T_PTROBJ_SPECIAL
  1760.               if (sv_derived_from($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")) {
  1761.                   IV tmp = SvIV((SV*)SvRV($arg));
  1762.               $var = ($type) tmp;
  1763.               }
  1764.               else
  1765.                   croak(\"$var is not of type ${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")
  1766.  
  1767.           OUTPUT
  1768.           T_PTROBJ_SPECIAL
  1769.               sv_setref_pv($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\",
  1770.               (void*)$var);
  1771.  
  1772.       The INPUT and    OUTPUT sections    substitute underscores for
  1773.       double-colons    on the fly, giving the desired effect.    This
  1774.       example demonstrates some of the power and versatility of
  1775.       the typemap facility.
  1776.  
  1777.  
  1778.  
  1779.      Page 27                        (printed 10/23/98)
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1787.  
  1788.  
  1789.  
  1790.      EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  1791.       File RPC.xs: Interface to some ONC+ RPC bind library
  1792.       functions.
  1793.  
  1794.            #include    "EXTERN.h"
  1795.            #include    "perl.h"
  1796.            #include    "XSUB.h"
  1797.  
  1798.            #include    <rpc/rpc.h>
  1799.  
  1800.            typedef struct netconfig    Netconfig;
  1801.  
  1802.            MODULE =    RPC  PACKAGE = RPC
  1803.  
  1804.            SV *
  1805.            rpcb_gettime(host="localhost")
  1806.             char *host
  1807.             PREINIT:
  1808.             time_t  timep;
  1809.             CODE:
  1810.             ST(0) = sv_newmortal();
  1811.             if(    rpcb_gettime( host, &timep ) )
  1812.              sv_setnv( ST(0), (double)timep    );
  1813.  
  1814.            Netconfig *
  1815.            getnetconfigent(netid="udp")
  1816.             char *netid
  1817.  
  1818.            MODULE =    RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_
  1819.  
  1820.            void
  1821.            rpcb_DESTROY(netconf)
  1822.             Netconfig *netconf
  1823.             CODE:
  1824.             printf("NetconfigPtr::DESTROY\n");
  1825.             free( netconf );
  1826.  
  1827.       File typemap:    Custom typemap for RPC.xs.
  1828.  
  1829.            TYPEMAP
  1830.            Netconfig *  T_PTROBJ
  1831.  
  1832.       File RPC.pm: Perl module for the RPC extension.
  1833.  
  1834.            package RPC;
  1835.  
  1836.            require Exporter;
  1837.            require DynaLoader;
  1838.            @ISA = qw(Exporter DynaLoader);
  1839.            @EXPORT = qw(rpcb_gettime getnetconfigent);
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.      Page 28                        (printed 10/23/98)
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1853.  
  1854.  
  1855.  
  1856.            bootstrap RPC;
  1857.            1;
  1858.  
  1859.       File rpctest.pl: Perl    test program for the RPC extension.
  1860.  
  1861.            use RPC;
  1862.  
  1863.            $netconf    = getnetconfigent();
  1864.            $a = rpcb_gettime();
  1865.            print "time = $a\n";
  1866.            print "netconf =    $netconf\n";
  1867.  
  1868.            $netconf    = getnetconfigent("tcp");
  1869.            $a = rpcb_gettime("poplar");
  1870.            print "time = $a\n";
  1871.            print "netconf =    $netconf\n";
  1872.  
  1873.  
  1874.      XXXXSSSS    VVVVEEEERRRRSSSSIIIIOOOONNNN
  1875.       This document    covers features    supported by xsubpp 1.935.
  1876.  
  1877.      AAAAUUUUTTTTHHHHOOOORRRR
  1878.       Dean Roehrich    <_r_o_e_h_r_i_c_h@_c_r_a_y._c_o_m> Jul    8, 1996
  1879.  
  1880.  
  1881.  
  1882.  
  1883.  
  1884.  
  1885.  
  1886.  
  1887.  
  1888.  
  1889.  
  1890.  
  1891.  
  1892.  
  1893.  
  1894.  
  1895.  
  1896.  
  1897.  
  1898.  
  1899.  
  1900.  
  1901.  
  1902.  
  1903.  
  1904.  
  1905.  
  1906.  
  1907.  
  1908.  
  1909.  
  1910.  
  1911.      Page 29                        (printed 10/23/98)
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.      PPPPEEEERRRRLLLLXXXXSSSS((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))         PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1919.  
  1920.  
  1921.  
  1922.  
  1923.  
  1924.  
  1925.  
  1926.  
  1927.  
  1928.  
  1929.  
  1930.  
  1931.  
  1932.  
  1933.  
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944.  
  1945.  
  1946.  
  1947.  
  1948.  
  1949.  
  1950.  
  1951.  
  1952.  
  1953.  
  1954.  
  1955.  
  1956.  
  1957.  
  1958.  
  1959.  
  1960.  
  1961.  
  1962.  
  1963.  
  1964.  
  1965.  
  1966.  
  1967.  
  1968.  
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974.      Page 30                        (printed 10/23/98)
  1975.  
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.